package com.yimi.yinhepda.views.main.my_module;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.SetOutLinePageAdapter;
import com.yimi.yinhepda.callbacks.EndlessRecyclerOnScrollListener;
import com.yimi.yinhepda.db.dao.LineVoTableDao;
import com.yimi.yinhepda.db.dao.UCDeptTableDao;
import com.yimi.yinhepda.db.table.DepartmentTable;
import com.yimi.yinhepda.db.table.LineVoTable;
import com.yimi.yinhepda.db.table.UCDeptTable;
import com.pda.uce.commom.util.LogUtil;
import com.yimi.yinhepda.base.BaseInitActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: lj
 * Date: 2019/10/24 16:40
 * Description: 出发线路界面
 */
public class SetOutLineActivity extends BaseInitActivity implements TextWatcher {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.et_next_dept)
    EditText etNextDept;
    @Bind(R.id.rv_set_out_line)
    RecyclerView rvSetOutLine;

    private List<DepartmentTable> mDepartmentList = new ArrayList<>();
    private SetOutLinePageAdapter mAdapter;
    private long mCurrentPage = 0;
    private long mPageSize = 100;

    @Override
    public String getPageName() {
        return "出发线路界面";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_set_out_line;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initEvent() {
        etNextDept.addTextChangedListener(this);
    }

    @Override
    public void initData() {
        tvTitle.setText("出发线路");

        Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<? super Void> subscriber) {
                //查询下一站
                mCurrentPage = 0;
                mDepartmentList.clear();
                mDepartmentList.addAll(queryNextDept(null, sharedPreferUtil.getDeptCode(), mCurrentPage, mPageSize));
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Void>() {
            @Override
            public void onCompleted() {
                if (!mDepartmentList.isEmpty()) {
                    mAdapter = new SetOutLinePageAdapter(mDepartmentList, null);
                    rvSetOutLine.setLayoutManager(new LinearLayoutManager(SetOutLineActivity.this, LinearLayoutManager.VERTICAL, false));
                    rvSetOutLine.setAdapter(mAdapter);
//                    rvSetOutLine.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
//                        @Override
//                        public void onLoadMore() {
////                            String nextDept = etNextDept.getText().toString().trim();
////                            mAdapter.setLoadState(SetOutLinePageAdapter.LOADING);
////                            //查询优速网点数据
////                            mCurrentPage++;
////                            List<UCDeptTable> ucDeptTableList = new UCDeptTableDao().findDataByPage(nextDept, mCurrentPage, mPageSize);
////                            if (ucDeptTableList != null && !ucDeptTableList.isEmpty()) {
////                                for (UCDeptTable item : ucDeptTableList) {
////                                    DepartmentTable departmentTable = new DepartmentTable();
////                                    departmentTable.setDeptCode(item.getBaseOrgCode());
////                                    departmentTable.setDeptName(item.getOrgName());
////                                    mDepartmentList.add(departmentTable);
////                                }
////                                mAdapter.setLoadState(SetOutLinePageAdapter.LOADING_COMPLETE);
////                                LogUtil.e("查询优速网点数据");
////                            } else {
////                                LogUtil.e("查询优速网点数据结束");
////                                // 显示加载到底的提示
////                                mAdapter.setLoadState(SetOutLinePageAdapter.LOADING_END);
////                            }
//                            mAdapter.setLoadState(SetOutLinePageAdapter.LOADING_END);
//                        }
//                    });
                    mAdapter.setLoadState(SetOutLinePageAdapter.LOADING_COMPLETE);
                } else {
                    showToast("未查询到出发线路！");
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onNext(Void aVoid) {
            }
        });
    }

    @OnClick({R.id.iv_back})
    @Override
    protected void onClickBtn(View v) {
        super.onClickBtn(v);
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(final CharSequence s, int start, int before, int count) {
        Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<? super Void> subscriber) {
                mDepartmentList.clear();
                if (!"".equals(s.toString().trim())) {
                    String nextDept = s.toString();
                    mCurrentPage = 0;
                    List<DepartmentTable> departmentTableList = queryNextDept(nextDept, sharedPreferUtil.getDeptCode(), mCurrentPage, mPageSize);
                    if (departmentTableList != null) {
                        mDepartmentList.addAll(departmentTableList);
                    }
                }
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Void>() {
            @Override
            public void onCompleted() {
                if (!"".equals(s.toString().trim())) {
                    mAdapter.notifyDataSetChanged();
                } else {
                    initData();
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onNext(Void aVoid) {
            }
        });
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    /**
     * 查询下一站
     */
    private List<DepartmentTable> queryNextDept(String deptNext, String deptCode, long currentPage, long pageSiz) {
        List<DepartmentTable> departmentList = new ArrayList<>();
        //查询下一站线路
        List<LineVoTable> lineList = new LineVoTableDao().findNextLine(deptCode, deptNext);
        if (!lineList.isEmpty()) {
            for (LineVoTable item : lineList) {
                DepartmentTable departmentTable = new DepartmentTable();
                departmentTable.setDeptCode(item.getDestZoneCode());
                departmentTable.setDeptName(item.getDestZoneName());
                departmentList.add(departmentTable);
            }
        }
//        //查询优速网点数据
//        List<UCDeptTable> ucDeptTableList = new UCDeptTableDao().findDataByPage(deptNext, currentPage, pageSiz);
//        for (UCDeptTable item : ucDeptTableList) {
//            DepartmentTable departmentTable = new DepartmentTable();
//            departmentTable.setDeptCode(item.getBaseOrgCode());
//            departmentTable.setDeptName(item.getOrgName());
//            departmentList.add(departmentTable);
//        }
        return departmentList;
    }
}
